package de.sendorian.app.forumArchive.dundjinni;


import static de.sendorian.app.forumArchive.matcher.ForumMatcher.*;
import static de.sendorian.app.forumArchive.renamer.Renamers.*;
import static org.hamcrest.Matchers.*;

import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.hamcrest.Matcher;

import de.sendorian.app.forumArchive.Config;
import de.sendorian.app.forumArchive.ConfigStrategy;
import de.sendorian.app.forumArchive.domain.Forum;
import de.sendorian.app.forumArchive.domain.Post;
import de.sendorian.app.forumArchive.domain.Topic;
import de.sendorian.app.forumArchive.finder.Finder;
import de.sendorian.app.forumArchive.renamer.FileRenamer;

public class DundjinniConfigStrategy extends Config implements ConfigStrategy {

	private static Logger logger = Logger.getLogger(DundjinniConfigStrategy.class.getName());

	private static final String BASE_URL = "http://www.dundjinni.com/forums/";

	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy h:mma");

	private Pattern DATE_PATTERN = Pattern
	    .compile("(\\d{2}/\\d{2}/\\d{4}).*at.*(\\d{1,2}:\\d{2}.m)");

	public String getBaseUrl() {
		return BASE_URL;
	}


	public SimpleDateFormat getDateFormat() {
		return DATE_FORMAT;
	}


	public Pattern getDatePattern() {
		return DATE_PATTERN;
	}


	public Finder<String, Forum> getForumFinder() {
		return new DundjinniForumFinder();
	}


	public Finder<Topic, Post> getPostFinder() {
		return new DundjinniPostFinder();
	}


	public Finder<Forum, Topic> getTopicFinder() {
		return new DundjinniTopicFinder();
	}


	public Matcher<Post> getPostMatcher() {
		if (getStartDate() != null && getEndDate() != null) {
			return matchesDateInterval(getStartDate(), getEndDate());
		}
		return anything();
	}


	public Matcher<Topic> getTopicMatcher() {
		if (getStartDate() != null) {
			return matchesDateIntervalUntilNow(getStartDate());
		}
		return anything();
	}


	public FileRenamer getDefaultRenamer() {
		return both(removeHash()).and(capitalizeFirst()).and(appendUserShortname()).and(
		    removeForbiddenChars());
	}

}
